package com.bjsxt.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjsxt.Service.TbContentService;
import com.bjsxt.commons.pojo.BaizhanResult;
import com.bjsxt.mapper.TbContentMapper;
import com.bjsxt.pojo.TbContent;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author coder_z
 * @date 2023/3/8
 **/
@Service
public class TbContentServiceImpl implements TbContentService {
    @Autowired
    private TbContentMapper tbContentMapper;
    @Override
    public BaizhanResult showContent(long categoryId) {
        QueryWrapper<TbContent> wrapper = new QueryWrapper<TbContent>().eq("category_id", categoryId);
        List<TbContent> list = tbContentMapper.selectList(wrapper);
        return BaizhanResult.ok(list);
    }

    @Override
    public BaizhanResult insert(TbContent content) {
        Date date = new Date();
        content.setCreated(date);
        content.setUpdated(date);
        int insert = tbContentMapper.insert(content);
        if(insert==1){
//            内容新增成功
//            保证项目结构完整性
//            使用Rabbitmq保证异步消息
            return BaizhanResult.ok();
        }
        return BaizhanResult.error("服务器消息，内容分类新增失败");
    }

    @Override
    public BaizhanResult deleteById(long id) {
        int index = tbContentMapper.deleteById(id);
        if(index==1){
            return  BaizhanResult.ok();
        }
        return BaizhanResult.error("服务器消息,内容删除失败");
    }
}
